﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename :D_T_EBook_Exp
//        description :对电子书籍进行操作
//
//        created by Jason at  2010-12-22 15：25
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using huaguwang;

namespace huaguwang
{
    public class D_T_EBook_Exp : D_T_EBook
    {
        public M_Page DRToMPage(DataRow dr, int len, int classlen, int nrlen, string url, string classurl, string dtFormat)
        {
            var model = new M_Page()
            {
                id = dr["id"].Exp_Trim(),
                name = dr["title"].Exp_Trim().Exp_LeftByt(len),
                title = dr["title"].Exp_Trim(),
                classid = dr["bid"].Exp_Trim(),
                classname = dr["catename"].Exp_Trim().Exp_LeftByt(classlen),
                classtitle = dr["catename"].Exp_Trim(),
                url = url.Exp_Format(dr["id"]),
                classurl = classurl.Exp_Format(dr["bid"]),
                date = dr["adddate"].Exp_DateTimeTryParse(),
                strdate = dr["adddate"].Exp_DateTimeTryParse().ToString(dtFormat),
                nr = dr["Explain"].Exp_Trim().Exp_RegexHTML().Exp_LeftByt(nrlen),
                click = dr["click"].Exp_Trim(),
                img1 = dr["pic"].Exp_Trim()
            };
            return model;

        }
        public enum OrderByType
        {
            date,
            hot,
            Topis
        }
        public string GetOrderByType(OrderByType e)
        {
            switch (e)
            {
                case OrderByType.date:
                    return "a.AddDate desc";
                case OrderByType.hot:
                    return "a.click desc";
                case OrderByType.Topis:
                    return "a.Topis desc ";
                default:
                    break;
            }
            return "a.id";
        }
        public List<M_Page> GetHotTop10(int len, int classlen, int nrlen, string url, string classurl, string dtFormat, int Top, OrderByType e)
        {
            var order = GetOrderByType(e);
            var strSql = " select Top " + Top + "  a.id,a.Title,a.AddDate,a.Explain,a.Click,a.Pic,bid=b.id,b.CateName " +
                        " from T_EBook a " +
                        " inner join T_Category b on a.CategoryID=b.id " +
                        " where a.Flag=1 order by " + order;
            var item = MSSQLHelper.SQLConType.C1.Exp_GetList<M_Page>(strSql,
                dr => DRToMPage(dr, len, classlen, nrlen, url, classurl, dtFormat));
            return item;
        }


        /// <summary>
        /// 异步分页的查询
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public int GetPage1Size(int type)
        {
            var strPageCount = ExpandMSSQL.GetPageSQL(
                "T_EBook a  inner join T_Category b on a.CategoryID=b.id ",
                " a.Flag=1 "
                );
            var pageCount = MSSQLHelper.SQLConType.C1.Exp_GetSingle(strPageCount).Exp_IntTryParse();
            return pageCount;
        }
        /// <summary>
        /// 异步分页的查询
        /// </summary>
        /// <param name="begin"></param>
        /// <param name="pagesize"></param>
        /// <param name="type"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public List<M_Page> GetPage1(int begin, int pagesize, int type, Func<M_Page, M_Page> func)
        {

            var strPage = ExpandMSSQL.GetPageSQL(begin, pagesize,
                "a.id,a.Title,a.URL,a.Pic,a.Explain,a.Click,a.Topis,b.id as classid,b.CateName as classname",
                "T_EBook a  inner join T_Category b on a.CategoryID=b.id ",
                " a.id ",
                " a.Flag=1 ",
                ""
                );


            var dt = MSSQLHelper.SQLConType.C1.Exp_GetList<M_Page>(
                strPage,
                dr =>
                {
                    var mo = ToMPage(dr);
                    if (func != null)
                    {
                        return func(mo);
                    }
                    return mo;
                }
                );
            return dt;

        }
        /// <summary>
        /// 复杂数据的公用转换方法
        /// </summary>
        /// <param name="DR"></param>
        /// <returns></returns>
        public M_Page ToMPage(DataRow DR)
        {
            var model = new M_Page
            {
                id = DR["id"].Exp_Trim(),
                name = DR["Title"].Exp_Trim(),
                title = DR["Title"].Exp_Trim(),
                nr = DR["Explain"].Exp_Trim(),
                img1 = DR["Pic"].Exp_Trim(),
                url = DR["id"].Exp_Trim(),
                classid = DR["classid"].Exp_Trim(),
                classname = DR["classname"].Exp_Trim(),
                classurl = DR["classid"].Exp_Trim(),
                click = DR["click"].Exp_Trim(),
            };
            return model;
        }
        public List<M_Page> GetOrderAll(int i, bool isClick)
        {
            var strSql = new StringBuilder();
            strSql.Append("  SELECT     TOP ({0})   id, Title, Pic, Explain, Click, Topis, t ");
            strSql.Append("  FROM         (SELECT     id, Title, Pic, Explain, Click, Topis, 'book' AS t ");
            strSql.Append("  FROM          (SELECT     TOP ({0}) id, Title, Pic, Explain, Click, Topis ");
            strSql.Append("  FROM          dbo.T_EBook AS a ");
            strSql.Append("  WHERE      (Flag = 1) ");
            strSql.Append("  ORDER BY {1} Click DESC) AS a_1 ");
            strSql.Append("  UNION ALL ");
            strSql.Append("  SELECT     id, Title, Pic, Explain, Click, Topis, 'video' AS t ");
            strSql.Append("  FROM         (SELECT     TOP ({0}) id, Title, Pic, Explain, Click, Topis ");
            strSql.Append("  FROM          dbo.T_Video AS a ");
            strSql.Append("  WHERE      (Flag = 1) ");
            strSql.Append("  ORDER BY {1} Click DESC) AS b ");
            strSql.Append("  UNION ALL ");
            strSql.Append("  SELECT     id, Title, Pic, Contents, Click, Topis, 'soft' AS t ");
            strSql.Append("  FROM         (SELECT     TOP ({0}) id, Title, Pic, Contents, Click, Topis ");
            strSql.Append("  FROM          dbo.T_SoftWare AS c ");
            strSql.Append("  WHERE      (Flag = 1) ");
            strSql.Append("  ORDER BY {1} Click DESC) AS c_1 ");
            strSql.Append("  UNION ALL ");
            strSql.Append("  SELECT     id, Title, PIC, Contents, Click, Topis, 'formula' AS t ");
            strSql.Append("  FROM         (SELECT     TOP ({0}) id, Title, PIC, Contents, Click, Topis ");
            strSql.Append("  FROM          dbo.T_Formula AS d ");
            strSql.Append("  WHERE      (Flag = 1) ");
            strSql.Append("  ORDER BY {1} Click DESC) d_1) AS a_2 ");
            strSql.Append("  ORDER BY {1} Click DESC  ");
            var strorder = (isClick ? " " : " Topis desc,");

            var str = strSql.ToString().Exp_Format(i, strorder);
            var dt = MSSQLHelper.SQLConType.C1.Exp_GetList<M_Page>(str, DR => new M_Page
            {
                id = DR["id"].Exp_Trim(),
                name = DR["Title"].Exp_Trim(),
                title = DR["Title"].Exp_Trim(),
                //nr = DR["Explain"].Exp_Trim(),
                img1 = DR["Pic"].Exp_Trim(),
                b1 = DR["t"].Exp_Trim()
            });
            return dt;
        }

        /// <summary>
        /// 获得当前电子书的总数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            var strSql = " select count(id) as c from T_EBook where Flag=1 ";
            var intC = MSSQLHelper.SQLConType.C1.Exp_GetSingle(strSql).Exp_IntTryParse();
            return intC;
        }
        /// <summary>
        /// 按照点击排序查找,或者推荐
        /// </summary>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public List<M_Page> GetOrder(int pagesize, bool? isClick, Func<M_Page, M_Page> funcPage)
        {

            var strSql = " select  {0} a.id,a.Title,a.URL,a.Pic,a.Explain,a.Click,a.Topis,b.id as classid,b.CateName as classname" +
                         " from T_EBook a " +
                         " inner join T_Category b on a.CategoryID=b.id " +
                         " where a.Flag=1 order by {1} ";


            // var strSql = "select {0} id,Title,Pic,Explain from T_EBook where Flag=1 order by {1} ";
            var strorder = "";
            if (isClick.HasValue)
            {
                strorder = (isClick.Value ? " a.Click desc " : " a.Topis desc,a.Click desc");
            }
            else
            {
                strorder = " a.id desc ";
            }

            if (pagesize > 0)
            {
                strSql = strSql.Exp_Format(" top " + pagesize, strorder);
            }
            else
            {
                strSql = strSql.Exp_Format(" ", strorder);
            }
            var dt = MSSQLHelper.SQLConType.C1.Exp_GetList<M_Page>(strSql, DR =>
            {
                var model = new M_Page
                {
                    id = DR["id"].Exp_Trim(),
                    name = DR["Title"].Exp_Trim(),
                    title = DR["Title"].Exp_Trim(),
                    nr = DR["Explain"].Exp_Trim(),
                    img1 = DR["Pic"].Exp_Trim(),
                    url = DR["id"].Exp_Trim(),
                    classid = DR["classid"].Exp_Trim(),
                    classname = DR["classname"].Exp_Trim(),
                    classurl = DR["classid"].Exp_Trim(),
                    click = DR["click"].Exp_Trim(),
                };
                if (funcPage != null)
                {
                    model = funcPage(model);
                }
                return model;
            }
                );
            return dt;
        }
        /// <summary>
        /// 获得某些类别的教材数量
        /// </summary>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public List<M_Page> GetClass(int pagesize)
        {
            var strSql = " select {0} a.id,a.CateName,COUNT(b.id) as c from T_Category a " +
                        " inner join T_EBook b on a.id=b.CategoryID" +
                        " where Depth=2 and b.Flag=1 " +
                        " group by a.id,a.CateName ";
            if (pagesize > 0)
            {
                strSql = strSql.Exp_Format(pagesize);
            }
            else
            {
                strSql = strSql.Exp_Format(" ");
            }

            var dt = MSSQLHelper.SQLConType.C1.Exp_GetList<M_Page>(strSql, DR => new M_Page
            {
                id = DR["id"].Exp_Trim(),
                name = DR["CateName"].Exp_Trim(),
                title = DR["Catename"].Exp_Trim(),
                b1 = DR["c"].Exp_Trim(),
            });
            return dt;
        }
        /// <summary>
        /// 获得某些类别的教材数量
        /// </summary>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public List<M_Page> GetClassAll(int pid)
        {
            var strSql = " select a.id,a.CateName,a.ChildPath from T_Category a where a.ParentId=@pid";
            var tempdt = MSSQLHelper.SQLConType.C1.Exp_Query(strSql, new SqlParameter("@pid", SqlDbType.Int) { Value = pid });
            var list = new List<M_Page>();
            var strsql1 = " select COUNT(1) from  T_EBook where flag=1 and  CategoryID in ({0})";
            foreach (DataRow dr in tempdt.Tables[0].Rows)
            {

                var tempi = MSSQLHelper.SQLConType.C1.Exp_GetSingle(strsql1.Exp_Format(dr["Childpath"]));
                var i = tempi.Exp_IntTryParse();
                if (i > 0)
                {
                    list.Add(new M_Page()
                    {

                        id = dr["id"].Exp_Trim(),
                        name = dr["CateName"].Exp_Trim(),
                        title = dr["CateName"].Exp_Trim(),
                        b1 = i.ToString()
                    });
                }
            }



            return list;
        }
        /// <summary>
        /// 分页获取数据量
        /// </summary>
        /// <returns></returns>
        public int GetPageCount(string txtBookName, string cate, string type)
        {
            string strSQL = ExpandMSSQL.GetPageSQL(
                "dbo.T_EBook  a inner join T_Category b on a.CategoryID = b.id ",
                "a.flag =1 and b.flag =1 and a.Title like '%'+@txtBookName+'%'");
            if (cate != "")
            {
                strSQL = strSQL + " and a.CategoryID ='" + cate + "'";
            }
            if (type != "-1")
            {
                strSQL = strSQL + " and a.HasBook =" + type;
            }
            SqlParameter[] parms = { 
                                   new SqlParameter("@txtBookName",SqlDbType.NVarChar,100)
                                   };
            parms[0].Value = txtBookName.Exp_Trim();
            var PageCount = MSSQLHelper.SQLConType.C1.Exp_GetSingle(strSQL, parms).Exp_IntTryParse();
            return PageCount;
        }

        /// <summary>
        /// 分页获取数据信息
        /// </summary>
        /// <param name="Begin">起始目录</param>
        /// <param name="Size">每页显示的数量</param>
        /// <returns></returns>
        public List<M_T_EBook> GetPageList(int Begin, int Size, string txtBookName, string cate, string type)
        {
            StringBuilder strwhere = new StringBuilder();
            strwhere.Append("a.flag =1 and b.flag =1 and a.Title like '%'+@txtBookName+'%'");
            if (cate != "")
            {
                strwhere.Append(" and a.CategoryID ='" + cate + "'");
            }
            if (type != "-1")
            {
                strwhere.Append(" and a.HasBook =" + type);
            }
            string strSQL = ExpandMSSQL.GetPageSQL(
                   Begin,
                   Size,
                   "a.id,a.Title,a.AddDate,a.Explain,b.CateName,a.HasBook",
                   "T_EBook a inner join T_Category b on a.CategoryID = b.id",
                   "a.id",
                  strwhere.ToString(),
                   "a.AddDate desc");
            SqlParameter[] parms = { 
                                   new SqlParameter("@txtBookName",SqlDbType.NVarChar,100)
                                   };
            parms[0].Value = txtBookName.Exp_Trim();
            var List = MSSQLHelper.SQLConType.C1.Exp_GetList(strSQL,
                DR => new M_T_EBook()
                {
                    id = DR["id"].Exp_IntTryParse(),
                    Title = DR["Title"].Exp_Trim().Exp_SubStringByLength(26, ""),
                    AddDate = DR["AddDate"].Exp_DateTimeTryParse(),
                    Explain = DR["Explain"].Exp_Trim().Exp_RegexHTML().Exp_SubStringByLength(40, ""),
                    ColorKey = DR["CateName"].Exp_Trim(),
                    HasBook = DR["HasBook"].Exp_BoolTryParse()
                }, parms);
            return List;
        }


        /// <summary>
        ///  新增电子书
        /// </summary>
        /// <param name="model">电子书实体</param>
        /// <param name="blnHasRealBook">是否有实体书</param>
        /// <param name="modelRealBook">实体书实体</param>
        /// <returns></returns>
        public int AddExp(huaguwang.M_T_EBook modelEbook, bool blnHasRealBook, huaguwang.M_T_RealBook modelRealBook)
        {
            if (!blnHasRealBook)//没有实体
            {
                return base.Add(modelEbook);
            }
            else
            {
                //事务方式
                var items = MSSQLHelper.SQLConType.C1.ExecuteSqlTran((com, tran) =>
                {
                    try
                    {
                        //插入电子书信息
                        StringBuilder strSQL = new StringBuilder();
                        strSQL.Append("insert into T_EBook(");
                        strSQL.Append("Title,CategoryID,URL,AddDate,Size,Type,Source,Pic,Explain,Grade,Gold,Click,WebTitle,WebSummary,WebKey,InteriorKey,HasBook,ColorKey,ColorTitle,Topis,RemSign,Flag,ExperInfotID,UserID)");
                        strSQL.Append(" values (");
                        strSQL.Append("@Title,@CategoryID,@URL,@AddDate,@Size,@Type,@Source,@Pic,@Explain,@Grade,@Gold,@Click,@WebTitle,@WebSummary,@WebKey,@InteriorKey,@HasBook,@ColorKey,@ColorTitle,@Topis,@RemSign,@Flag,@ExperInfotID,@UserID)");
                        strSQL.Append(";select @@IDENTITY");

                        SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@URL", SqlDbType.NVarChar,100),
					new SqlParameter("@AddDate", SqlDbType.SmallDateTime),
					new SqlParameter("@Size", SqlDbType.Int,4),
					new SqlParameter("@Type", SqlDbType.NVarChar,50),
					new SqlParameter("@Source", SqlDbType.NVarChar,100),
					new SqlParameter("@Pic", SqlDbType.NVarChar,100),
					new SqlParameter("@Explain", SqlDbType.NText),
					new SqlParameter("@Grade", SqlDbType.Int,4),
					new SqlParameter("@Gold", SqlDbType.Money,8),
					new SqlParameter("@Click", SqlDbType.Int,4),
					new SqlParameter("@WebTitle", SqlDbType.NVarChar,100),
					new SqlParameter("@WebSummary", SqlDbType.NVarChar,100),
					new SqlParameter("@WebKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@InteriorKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@HasBook", SqlDbType.Bit,1),
					new SqlParameter("@ColorKey", SqlDbType.NText),
					new SqlParameter("@ColorTitle", SqlDbType.NText),
					new SqlParameter("@Topis", SqlDbType.Int,4),
					new SqlParameter("@RemSign", SqlDbType.Bit,1),
					new SqlParameter("@Flag", SqlDbType.Bit,1),
					new SqlParameter("@ExperInfotID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4)};
                        parameters[0].Value = modelEbook.Title;
                        parameters[1].Value = modelEbook.CategoryID;
                        parameters[2].Value = modelEbook.URL;
                        parameters[3].Value = modelEbook.AddDate;
                        parameters[4].Value = modelEbook.Size;
                        parameters[5].Value = modelEbook.Type;
                        parameters[6].Value = modelEbook.Source;
                        parameters[7].Value = modelEbook.Pic;
                        parameters[8].Value = modelEbook.Explain;
                        parameters[9].Value = modelEbook.Grade;
                        parameters[10].Value = modelEbook.Gold;
                        parameters[11].Value = modelEbook.Click;
                        parameters[12].Value = modelEbook.WebTitle;
                        parameters[13].Value = modelEbook.WebSummary;
                        parameters[14].Value = modelEbook.WebKey;
                        parameters[15].Value = modelEbook.InteriorKey;
                        parameters[16].Value = modelEbook.HasBook;
                        parameters[17].Value = modelEbook.ColorKey;
                        parameters[18].Value = modelEbook.ColorTitle;
                        parameters[19].Value = modelEbook.Topis;
                        parameters[20].Value = modelEbook.RemSign;
                        //flag标记在增加的使用永远为true
                        parameters[21].Value = true;
                        parameters[22].Value = modelEbook.ExperInfotID;
                        parameters[23].Value = modelEbook.UserID;

                        com.Parameters.Add(parameters[0]);
                        com.Parameters.Add(parameters[1]);
                        com.Parameters.Add(parameters[2]);
                        com.Parameters.Add(parameters[3]);
                        com.Parameters.Add(parameters[4]);
                        com.Parameters.Add(parameters[5]);
                        com.Parameters.Add(parameters[6]);
                        com.Parameters.Add(parameters[7]);
                        com.Parameters.Add(parameters[8]);
                        com.Parameters.Add(parameters[9]);
                        com.Parameters.Add(parameters[10]);
                        com.Parameters.Add(parameters[11]);
                        com.Parameters.Add(parameters[12]);
                        com.Parameters.Add(parameters[13]);
                        com.Parameters.Add(parameters[14]);
                        com.Parameters.Add(parameters[15]);
                        com.Parameters.Add(parameters[16]);
                        com.Parameters.Add(parameters[17]);
                        com.Parameters.Add(parameters[18]);
                        com.Parameters.Add(parameters[19]);
                        com.Parameters.Add(parameters[20]);
                        com.Parameters.Add(parameters[21]);
                        com.Parameters.Add(parameters[22]);
                        com.Parameters.Add(parameters[23]);
                        com.CommandText = strSQL.ToString();
                        int intID = com.ExecuteScalar().Exp_IntTryParse();//得到所添加的电子书ID
                        com.Parameters.Clear();

                        if (0 >= intID)
                        {
                            tran.Rollback();
                            return -1;
                        }
                        modelRealBook.id = intID;

                        //插入实体书信息
                        strSQL = strSQL = new StringBuilder();
                        strSQL.Append("insert into T_RealBook(");
                        strSQL.Append("Title,EbookID,ISBN,Publish,Author,Explain,Pic,Price,Gold,Grade,Click,WebTitle,WebSummary,WebKey,InnerKey,ColorTitle,ColorKey,SellNum,StockNum,RemSign,Topis,Flag)");
                        strSQL.Append(" values (");
                        strSQL.Append("@Title,@EbookID,@ISBN,@Publish,@Author,@Explain,@Pic,@Price,@Gold,@Grade,@Click,@WebTitle,@WebSummary,@WebKey,@InnerKey,@ColorTitle,@ColorKey,@SellNum,@StockNum,@RemSign,@Topis,@Flag)");
                        strSQL.Append(";select @@IDENTITY");
                        parameters = new SqlParameter[]{
				            new SqlParameter("@Title", SqlDbType.NVarChar,100),
					        new SqlParameter("@EbookID", SqlDbType.Int,4),
					        new SqlParameter("@ISBN", SqlDbType.NVarChar,50),
					        new SqlParameter("@Publish", SqlDbType.NVarChar,100),
					        new SqlParameter("@Author", SqlDbType.NVarChar,50),
					        new SqlParameter("@Explain", SqlDbType.NText),
					        new SqlParameter("@Pic", SqlDbType.NVarChar,255),
					        new SqlParameter("@Price", SqlDbType.Money,8),
					        new SqlParameter("@Gold", SqlDbType.Money,8),
					        new SqlParameter("@Grade", SqlDbType.Int,4),
					        new SqlParameter("@Click", SqlDbType.Int,4),
					        new SqlParameter("@WebTitle", SqlDbType.NVarChar,100),
					        new SqlParameter("@WebSummary", SqlDbType.NVarChar,1000),
					        new SqlParameter("@WebKey", SqlDbType.NVarChar,1000),
					        new SqlParameter("@InnerKey", SqlDbType.NVarChar,1000),
					        new SqlParameter("@ColorTitle", SqlDbType.NText),
					        new SqlParameter("@ColorKey", SqlDbType.NText),
					        new SqlParameter("@SellNum", SqlDbType.Int,4),
					        new SqlParameter("@StockNum", SqlDbType.Int,4),
					        new SqlParameter("@RemSign", SqlDbType.Bit,1),
					        new SqlParameter("@Topis", SqlDbType.Int,4),
					        new SqlParameter("@Flag", SqlDbType.Bit,1)};
                        parameters[0].Value = modelRealBook.Title;
                        parameters[1].Value = intID;
                        parameters[2].Value = modelRealBook.ISBN;
                        parameters[3].Value = modelRealBook.Publish;
                        parameters[4].Value = modelRealBook.Author;
                        parameters[5].Value = modelRealBook.Explain;
                        parameters[6].Value = modelRealBook.Pic;
                        parameters[7].Value = modelRealBook.Price;
                        parameters[8].Value = modelRealBook.Gold;
                        parameters[9].Value = modelRealBook.Grade;
                        parameters[10].Value = modelRealBook.Click;
                        parameters[11].Value = modelRealBook.WebTitle;
                        parameters[12].Value = modelRealBook.WebSummary;
                        parameters[13].Value = modelRealBook.WebKey;
                        parameters[14].Value = modelRealBook.InnerKey;
                        parameters[15].Value = modelRealBook.ColorTitle;
                        parameters[16].Value = modelRealBook.ColorKey;
                        parameters[17].Value = modelRealBook.SellNum;
                        parameters[18].Value = modelRealBook.StockNum;
                        parameters[19].Value = modelRealBook.RemSign;
                        parameters[20].Value = modelRealBook.Topis;
                        //flag标记在增加的使用永远为true
                        parameters[21].Value = true;

                        com.Parameters.Add(parameters[0]);
                        com.Parameters.Add(parameters[1]);
                        com.Parameters.Add(parameters[2]);
                        com.Parameters.Add(parameters[3]);
                        com.Parameters.Add(parameters[4]);
                        com.Parameters.Add(parameters[5]);
                        com.Parameters.Add(parameters[6]);
                        com.Parameters.Add(parameters[7]);
                        com.Parameters.Add(parameters[8]);
                        com.Parameters.Add(parameters[9]);
                        com.Parameters.Add(parameters[10]);
                        com.Parameters.Add(parameters[11]);
                        com.Parameters.Add(parameters[12]);
                        com.Parameters.Add(parameters[13]);
                        com.Parameters.Add(parameters[14]);
                        com.Parameters.Add(parameters[15]);
                        com.Parameters.Add(parameters[16]);
                        com.Parameters.Add(parameters[17]);
                        com.Parameters.Add(parameters[18]);
                        com.Parameters.Add(parameters[19]);
                        com.Parameters.Add(parameters[20]);
                        com.Parameters.Add(parameters[21]);
                        com.CommandText = strSQL.ToString();

                        intID = com.ExecuteScalar().Exp_IntTryParse();
                        com.Parameters.Clear();
                        if (0 >= intID)
                        {
                            tran.Rollback();
                            return -2;
                        }
                    }
                    catch
                    {
                        tran.Rollback();
                        return 0;
                    }
                    tran.Commit();
                    return modelRealBook.id;
                });
                return items;
            }
        }

        /// <summary>
        /// 修改书籍信息
        /// </summary>
        /// <param name="EBookModel">电子书实体</param>
        /// <param name="blnHasRealBook">是否有实体书</param>
        /// <param name="RealBookModel">实体书实体</param>
        /// <returns></returns>
        public int UpdateExp(huaguwang.M_T_EBook EBookModel, bool blnHasRealBook, huaguwang.M_T_RealBook RealBookModel)
        {
            if (!blnHasRealBook)//没有实体书实体
            {
                return base.Update(EBookModel);
            }
            else
            {
                //事务方式
                var items = MSSQLHelper.SQLConType.C1.ExecuteSqlTran((com, tran) =>
                {
                    try
                    {
                        //修改视频信息
                        StringBuilder strSQL = new StringBuilder();
                        strSQL.Append("update T_EBook set ");
                        strSQL.Append("Title=@Title,");
                        strSQL.Append("CategoryID=@CategoryID,");
                        strSQL.Append("URL=@URL,");
                        strSQL.Append("AddDate=@AddDate,");
                        strSQL.Append("Size=@Size,");
                        strSQL.Append("Type=@Type,");
                        strSQL.Append("Source=@Source,");
                        strSQL.Append("Pic=@Pic,");
                        strSQL.Append("Explain=@Explain,");
                        strSQL.Append("Grade=@Grade,");
                        strSQL.Append("Gold=@Gold,");
                        strSQL.Append("Click=@Click,");
                        strSQL.Append("WebTitle=@WebTitle,");
                        strSQL.Append("WebSummary=@WebSummary,");
                        strSQL.Append("WebKey=@WebKey,");
                        strSQL.Append("InteriorKey=@InteriorKey,");
                        strSQL.Append("HasBook=@HasBook,");
                        strSQL.Append("ColorKey=@ColorKey,");
                        strSQL.Append("ColorTitle=@ColorTitle,");
                        strSQL.Append("Topis=@Topis,");
                        strSQL.Append("RemSign=@RemSign,");
                        strSQL.Append("ExperInfotID=@ExperInfotID,");
                        strSQL.Append("UserID=@UserID");
                        strSQL.Append(" where id=@id");
                        SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@CategoryID", SqlDbType.Int,4),
					new SqlParameter("@URL", SqlDbType.NVarChar,100),
					new SqlParameter("@AddDate", SqlDbType.SmallDateTime),
					new SqlParameter("@Size", SqlDbType.Int,4),
					new SqlParameter("@Type", SqlDbType.NVarChar,50),
					new SqlParameter("@Source", SqlDbType.NVarChar,100),
					new SqlParameter("@Pic", SqlDbType.NVarChar,100),
					new SqlParameter("@Explain", SqlDbType.NText),
					new SqlParameter("@Grade", SqlDbType.Int,4),
					new SqlParameter("@Gold", SqlDbType.Money,8),
					new SqlParameter("@Click", SqlDbType.Int,4),
					new SqlParameter("@WebTitle", SqlDbType.NVarChar,100),
					new SqlParameter("@WebSummary", SqlDbType.NVarChar,100),
					new SqlParameter("@WebKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@InteriorKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@HasBook", SqlDbType.Bit,1),
					new SqlParameter("@ColorKey", SqlDbType.NText),
					new SqlParameter("@ColorTitle", SqlDbType.NText),
					new SqlParameter("@Topis", SqlDbType.Int,4),
					new SqlParameter("@RemSign", SqlDbType.Bit,1),
					new SqlParameter("@Flag", SqlDbType.Bit,1),
					new SqlParameter("@ExperInfotID", SqlDbType.Int,4),
					new SqlParameter("@UserID", SqlDbType.Int,4)};
                        parameters[0].Value = EBookModel.id;
                        parameters[1].Value = EBookModel.Title;
                        parameters[2].Value = EBookModel.CategoryID;
                        parameters[3].Value = EBookModel.URL;
                        parameters[4].Value = EBookModel.AddDate;
                        parameters[5].Value = EBookModel.Size;
                        parameters[6].Value = EBookModel.Type;
                        parameters[7].Value = EBookModel.Source;
                        parameters[8].Value = EBookModel.Pic;
                        parameters[9].Value = EBookModel.Explain;
                        parameters[10].Value = EBookModel.Grade;
                        parameters[11].Value = EBookModel.Gold;
                        parameters[12].Value = EBookModel.Click;
                        parameters[13].Value = EBookModel.WebTitle;
                        parameters[14].Value = EBookModel.WebSummary;
                        parameters[15].Value = EBookModel.WebKey;
                        parameters[16].Value = EBookModel.InteriorKey;
                        parameters[17].Value = true;
                        parameters[18].Value = EBookModel.ColorKey;
                        parameters[19].Value = EBookModel.ColorTitle;
                        parameters[20].Value = EBookModel.Topis;
                        parameters[21].Value = EBookModel.RemSign;
                        parameters[22].Value = EBookModel.Flag;
                        parameters[23].Value = EBookModel.ExperInfotID;
                        parameters[24].Value = EBookModel.UserID;
                        com.Parameters.Add(parameters[0]);
                        com.Parameters.Add(parameters[1]);
                        com.Parameters.Add(parameters[2]);
                        com.Parameters.Add(parameters[3]);
                        com.Parameters.Add(parameters[4]);
                        com.Parameters.Add(parameters[5]);
                        com.Parameters.Add(parameters[6]);
                        com.Parameters.Add(parameters[7]);
                        com.Parameters.Add(parameters[8]);
                        com.Parameters.Add(parameters[9]);
                        com.Parameters.Add(parameters[10]);
                        com.Parameters.Add(parameters[11]);
                        com.Parameters.Add(parameters[12]);
                        com.Parameters.Add(parameters[13]);
                        com.Parameters.Add(parameters[14]);
                        com.Parameters.Add(parameters[15]);
                        com.Parameters.Add(parameters[16]);
                        com.Parameters.Add(parameters[17]);
                        com.Parameters.Add(parameters[18]);
                        com.Parameters.Add(parameters[19]);
                        com.Parameters.Add(parameters[20]);
                        com.Parameters.Add(parameters[21]);
                        com.Parameters.Add(parameters[22]);
                        com.Parameters.Add(parameters[23]);
                        com.Parameters.Add(parameters[24]);
                        com.CommandText = strSQL.ToString();
                        int intID = com.ExecuteNonQuery().Exp_IntTryParse();//执行SQL语句，返回记录数
                        com.Parameters.Clear();

                        if (0 >= intID)
                        {
                            tran.Rollback();//回滚
                            return -1;
                        }
                        strSQL = new StringBuilder();
                        strSQL.Append("select count(1) from T_RealBook where EbookID = " + EBookModel.id + " and Flag = 1 ");
                        com.CommandText = strSQL.ToString();
                        int iRsult = com.ExecuteScalar().Exp_IntTryParse();
                        if (iRsult > 0)//如果已经存在实体书信息就更新实体书信息
                        {
                            strSQL = strSQL = new StringBuilder();
                            strSQL.Append("update T_RealBook set ");
                            strSQL.Append("Title=@Title,");
                            strSQL.Append("EbookID=@EbookID,");
                            strSQL.Append("ISBN=@ISBN,");
                            strSQL.Append("Publish=@Publish,");
                            strSQL.Append("Author=@Author,");
                            strSQL.Append("Explain=@Explain,");
                            strSQL.Append("Pic=@Pic,");
                            strSQL.Append("Price=@Price,");
                            strSQL.Append("Gold=@Gold,");
                            strSQL.Append("Grade=@Grade,");
                            strSQL.Append("Click=@Click,");
                            strSQL.Append("WebTitle=@WebTitle,");
                            strSQL.Append("WebSummary=@WebSummary,");
                            strSQL.Append("WebKey=@WebKey,");
                            strSQL.Append("InnerKey=@InnerKey,");
                            strSQL.Append("ColorTitle=@ColorTitle,");
                            strSQL.Append("ColorKey=@ColorKey,");
                            strSQL.Append("SellNum=@SellNum,");
                            strSQL.Append("StockNum=@StockNum,");
                            strSQL.Append("RemSign=@RemSign,");
                            strSQL.Append("Topis=@Topis");
                            strSQL.Append(" where EbookID=@id");
                            parameters = new SqlParameter[] {
					new SqlParameter("@id", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@EbookID", SqlDbType.Int,4),
					new SqlParameter("@ISBN", SqlDbType.NVarChar,50),
					new SqlParameter("@Publish", SqlDbType.NVarChar,100),
					new SqlParameter("@Author", SqlDbType.NVarChar,50),
					new SqlParameter("@Explain", SqlDbType.NText),
					new SqlParameter("@Pic", SqlDbType.NVarChar,255),
					new SqlParameter("@Price", SqlDbType.Money,8),
					new SqlParameter("@Gold", SqlDbType.Money,8),
					new SqlParameter("@Grade", SqlDbType.Int,4),
					new SqlParameter("@Click", SqlDbType.Int,4),
					new SqlParameter("@WebTitle", SqlDbType.NVarChar,100),
					new SqlParameter("@WebSummary", SqlDbType.NVarChar,1000),
					new SqlParameter("@WebKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@InnerKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@ColorTitle", SqlDbType.NText),
					new SqlParameter("@ColorKey", SqlDbType.NText),
					new SqlParameter("@SellNum", SqlDbType.Int,4),
					new SqlParameter("@StockNum", SqlDbType.Int,4),
					new SqlParameter("@RemSign", SqlDbType.Bit,1),
					new SqlParameter("@Topis", SqlDbType.Int,4),
					new SqlParameter("@Flag", SqlDbType.Bit,1)};
                            //
                            parameters[0].Value = EBookModel.id;
                            parameters[1].Value = RealBookModel.Title;
                            parameters[2].Value = RealBookModel.EbookID;
                            parameters[3].Value = RealBookModel.ISBN;
                            parameters[4].Value = RealBookModel.Publish;
                            parameters[5].Value = RealBookModel.Author;
                            parameters[6].Value = RealBookModel.Explain;
                            parameters[7].Value = RealBookModel.Pic;
                            parameters[8].Value = RealBookModel.Price;
                            parameters[9].Value = RealBookModel.Gold;
                            parameters[10].Value = RealBookModel.Grade;
                            parameters[11].Value = RealBookModel.Click;
                            parameters[12].Value = RealBookModel.WebTitle;
                            parameters[13].Value = RealBookModel.WebSummary;
                            parameters[14].Value = RealBookModel.WebKey;
                            parameters[15].Value = RealBookModel.InnerKey;
                            parameters[16].Value = RealBookModel.ColorTitle;
                            parameters[17].Value = RealBookModel.ColorKey;
                            parameters[18].Value = RealBookModel.SellNum;
                            parameters[19].Value = RealBookModel.StockNum;
                            parameters[20].Value = RealBookModel.RemSign;
                            parameters[21].Value = RealBookModel.Topis;
                            parameters[22].Value = RealBookModel.Flag;

                            com.Parameters.Add(parameters[0]);
                            com.Parameters.Add(parameters[1]);
                            com.Parameters.Add(parameters[2]);
                            com.Parameters.Add(parameters[3]);
                            com.Parameters.Add(parameters[4]);
                            com.Parameters.Add(parameters[5]);
                            com.Parameters.Add(parameters[6]);
                            com.Parameters.Add(parameters[7]);
                            com.Parameters.Add(parameters[8]);
                            com.Parameters.Add(parameters[9]);
                            com.Parameters.Add(parameters[10]);
                            com.Parameters.Add(parameters[11]);
                            com.Parameters.Add(parameters[12]);
                            com.Parameters.Add(parameters[13]);
                            com.Parameters.Add(parameters[14]);
                            com.Parameters.Add(parameters[15]);
                            com.Parameters.Add(parameters[16]);
                            com.Parameters.Add(parameters[17]);
                            com.Parameters.Add(parameters[18]);
                            com.Parameters.Add(parameters[19]);
                            com.Parameters.Add(parameters[20]);
                            com.Parameters.Add(parameters[21]);
                            com.Parameters.Add(parameters[22]);
                            com.CommandText = strSQL.ToString();
                            intID = com.ExecuteNonQuery().Exp_IntTryParse();
                            com.Parameters.Clear();
                            if (0 >= intID)
                            {
                                tran.Rollback();
                                return -2;
                            }
                        }
                        else  //如果没有实体书信息就添加实体书信息
                        {
                            strSQL = strSQL = new StringBuilder();
                            strSQL.Append("insert into T_RealBook(");
                            strSQL.Append("Title,EbookID,ISBN,Publish,Author,Explain,Pic,Price,Gold,Grade,Click,WebTitle,WebSummary,WebKey,InnerKey,ColorTitle,ColorKey,SellNum,StockNum,RemSign,Topis,Flag)");
                            strSQL.Append(" values (");
                            strSQL.Append("@Title,@EbookID,@ISBN,@Publish,@Author,@Explain,@Pic,@Price,@Gold,@Grade,@Click,@WebTitle,@WebSummary,@WebKey,@InnerKey,@ColorTitle,@ColorKey,@SellNum,@StockNum,@RemSign,@Topis,@Flag)");
                            strSQL.Append(";select @@IDENTITY");
                            parameters = new SqlParameter[] {
				            new SqlParameter("@Title", SqlDbType.NVarChar,100),
					        new SqlParameter("@EbookID", SqlDbType.Int,4),
					        new SqlParameter("@ISBN", SqlDbType.NVarChar,50),
					        new SqlParameter("@Publish", SqlDbType.NVarChar,100),
					        new SqlParameter("@Author", SqlDbType.NVarChar,50),
					        new SqlParameter("@Explain", SqlDbType.NText),
					        new SqlParameter("@Pic", SqlDbType.NVarChar,255),
					        new SqlParameter("@Price", SqlDbType.Money,8),
					        new SqlParameter("@Gold", SqlDbType.Money,8),
					        new SqlParameter("@Grade", SqlDbType.Int,4),
					        new SqlParameter("@Click", SqlDbType.Int,4),
					        new SqlParameter("@WebTitle", SqlDbType.NVarChar,100),
					        new SqlParameter("@WebSummary", SqlDbType.NVarChar,1000),
					        new SqlParameter("@WebKey", SqlDbType.NVarChar,1000),
					        new SqlParameter("@InnerKey", SqlDbType.NVarChar,1000),
					        new SqlParameter("@ColorTitle", SqlDbType.NText),
					        new SqlParameter("@ColorKey", SqlDbType.NText),
					        new SqlParameter("@SellNum", SqlDbType.Int,4),
					        new SqlParameter("@StockNum", SqlDbType.Int,4),
					        new SqlParameter("@RemSign", SqlDbType.Bit,1),
					        new SqlParameter("@Topis", SqlDbType.Int,4),
					        new SqlParameter("@Flag", SqlDbType.Bit,1)};
                            parameters[0].Value = RealBookModel.Title;
                            //实体书EbookID =》EbookModel.id
                            parameters[1].Value = EBookModel.id;
                            parameters[2].Value = RealBookModel.ISBN;
                            parameters[3].Value = RealBookModel.Publish;
                            parameters[4].Value = RealBookModel.Author;
                            parameters[5].Value = RealBookModel.Explain;
                            parameters[6].Value = RealBookModel.Pic;
                            parameters[7].Value = RealBookModel.Price;
                            parameters[8].Value = RealBookModel.Gold;
                            parameters[9].Value = RealBookModel.Grade;
                            parameters[10].Value = RealBookModel.Click;
                            parameters[11].Value = RealBookModel.WebTitle;
                            parameters[12].Value = RealBookModel.WebSummary;
                            parameters[13].Value = RealBookModel.WebKey;
                            parameters[14].Value = RealBookModel.InnerKey;
                            parameters[15].Value = RealBookModel.ColorTitle;
                            parameters[16].Value = RealBookModel.ColorKey;
                            parameters[17].Value = RealBookModel.SellNum;
                            parameters[18].Value = RealBookModel.StockNum;
                            parameters[19].Value = RealBookModel.RemSign;
                            parameters[20].Value = RealBookModel.Topis;
                            //flag标记在增加的使用永远为true
                            parameters[21].Value = true;

                            com.Parameters.Add(parameters[0]);
                            com.Parameters.Add(parameters[1]);
                            com.Parameters.Add(parameters[2]);
                            com.Parameters.Add(parameters[3]);
                            com.Parameters.Add(parameters[4]);
                            com.Parameters.Add(parameters[5]);
                            com.Parameters.Add(parameters[6]);
                            com.Parameters.Add(parameters[7]);
                            com.Parameters.Add(parameters[8]);
                            com.Parameters.Add(parameters[9]);
                            com.Parameters.Add(parameters[10]);
                            com.Parameters.Add(parameters[11]);
                            com.Parameters.Add(parameters[12]);
                            com.Parameters.Add(parameters[13]);
                            com.Parameters.Add(parameters[14]);
                            com.Parameters.Add(parameters[15]);
                            com.Parameters.Add(parameters[16]);
                            com.Parameters.Add(parameters[17]);
                            com.Parameters.Add(parameters[18]);
                            com.Parameters.Add(parameters[19]);
                            com.Parameters.Add(parameters[20]);
                            com.Parameters.Add(parameters[21]);
                            com.CommandText = strSQL.ToString();
                            intID = com.ExecuteScalar().Exp_IntTryParse();
                            com.Parameters.Clear();
                            if (0 >= intID)
                            {
                                tran.Rollback();
                                return -3;
                            }
                        }
                    }
                    catch
                    {
                        tran.Rollback();
                        return 0;
                    }
                    tran.Commit();
                    return EBookModel.id;
                });
                return items;
            }
        }
    }
}
